/**
 * createAuthor: jaden.liu
 * createTime: 2019-12-07
 * version: 1.0.0
 */

/**
 * 查询数据
 */
var searchBean = function () {
		var params = LQ.getFormValue('#tb');
		$('#gridBean').datagrid('options').queryParams = params;
		$('#gridBean').datagrid('reload');
};

/**
 * 双击datagrid行事件
 * @param rowIndex
 * @param rowData
 */
var gridBean_DblClickRow = function (rowIndex, rowData) {
		//通过主键查询用户信息
		getByUserId(rowData.id, function (resp) {
				$('#dlgBean02').dialog('open');
				$('#formBean02').form('clear');
				$('#formBean02').form('load', resp.data);
		});
};

/* --------------------------------新增----------------------------------------------- */

/**
 * 新增按钮处理
 */
var addHandler = function () {
		//数据初始化
		$('#formBean').form('load', {});
		$('#dlgBean').dialog('open');
};

/**
 * 新增数据
 */
var submitForm = function () {
		$('#formBean').form('validate');
		$.ajax({
				url: '/article/save.json',
				type: 'post',
				data: LQ.getFormStr('#formBean'),
				contentType: "application/json",
				dataType: 'json',
				success: function (resp) {
						layer.msg('保存成功', {time: 3000});
						searchBean();//查询数据
						$('#dlgBean').dialog('close');
				}
		});
};

/**
 * 重置新增弹出框
 */
var clearForm = function () {
		$('#formBean').form('clear');
};

/* --------------------------------修改----------------------------------------------- */

/**
 * 通过主键查询用户信息
 */
var getByUserId = function (userId, callback) {
		$.ajax({
				url: '/article/one.json?id=' + userId,
				type: 'get',
				dataType: 'json',
				success: function (resp) {
						callback && callback(resp);
				},
		});
};

/**
 * 修改数据加载处理
 */
var updateHandler = function () {
		var row = $('#gridBean').datagrid('getSelected');
		if (row == null) {
				layer.msg('请选中需要修改的行！', {time: 3000});
				return;
		}
		//通过主键查询用户信息
		getByUserId(row.id, function (resp) {
				$('#dlgBean02').dialog('open');
				$('#formBean02').form('clear');
				$('#formBean02').form('load', resp.data);
		});
};
/**
 * 修改弹出框保存数据
 */
var updateSubmit = function () {
		$('#formBean02').form('validate');
		$.ajax({
				url: '/article/update.json',
				type: 'put',
				data: LQ.getFormStr('#formBean02'),
				contentType: "application/json",
				dataType: 'json',
				success: function (resp) {
						layer.msg('保存成功', {time: 3000});
						searchBean();//查询数据
						$('#dlgBean02').dialog('close');
				}
		});
};
/**
 * 修改弹出框清除数据
 */
var updateClear = function () {
		$('#formBean02').form('clear');
};

/* --------------------------------删除----------------------------------------------- */

/**
 * 删除用户信息
 */
var deleteHandler = function () {
		var row = $('#gridBean').datagrid('getSelected');
		if (row == null) {
				layer.msg('请选中需要修改的行！', {time: 3000});
				return;
		}
		layer.confirm('您确定要删除该数据吗？', {
				btn: ['确定', '取消'], //按钮
				title: "提示"
		}, function (index, layero) {
				$.ajax({
						url: '/article/delete.json?id=' + row.id,
						type: 'delete',
						contentType: "application/json",
						dataType: 'json',
						success: function (resp) {
								layer.msg('删除成功', {time: 3000});
								searchBean();//查询数据
						}
				});
		});
};


